package org.web.standardplatform.client.helper;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class AnnotationMinMaxExtractor {

    public static class MinMaxResult {
        private Integer minValue;
        private Integer maxValue;

        public Integer getMinValue() {
            return minValue;
        }

        public void setMinValue(Integer minValue) {
            this.minValue = minValue;
        }

        public Integer getMaxValue() {
            return maxValue;
        }

        public void setMaxValue(Integer maxValue) {
            this.maxValue = maxValue;
        }

        @Override
        public String toString() {
            return "MinMaxResult{" +
                    "minValue=" + minValue +
                    ", maxValue=" + maxValue +
                    '}';
        }
    }

    public static MinMaxResult extract(List<String> annotations) {
        MinMaxResult result = new MinMaxResult();

        if (annotations == null) {
            return result;
        }

        Pattern minPattern = Pattern.compile("@Min\\(.*?value\\s*=\\s*(\\d+).*?\\)");
        Pattern maxPattern = Pattern.compile("@Max\\(.*?value\\s*=\\s*(\\d+).*?\\)");

        for (String annotation : annotations) {
            Matcher minMatcher = minPattern.matcher(annotation);
            if (minMatcher.find()) {
                result.setMinValue(Integer.parseInt(minMatcher.group(1)));
            }

            Matcher maxMatcher = maxPattern.matcher(annotation);
            if (maxMatcher.find()) {
                result.setMaxValue(Integer.parseInt(maxMatcher.group(1)));
            }
        }

        return result;
    }
}
